package leeon.mobile.autoip;

import leeon.mobile.autoip.IPUtil.IpSetting;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.SupplicantState;
import android.net.wifi.WifiManager;
import android.provider.Settings;
import android.util.Log;
import android.widget.Toast;

public class WifiStateReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction(); 
		Log.i("Wifi", action);
		if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)) {
			boolean bs = intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false);
			Log.i("Wifi", "test connection change:" + bs);
			if (!bs) return;
		}
		if (action.equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)) {
			SupplicantState state = (SupplicantState)intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE);
			Log.i("Wifi", "test state change:" + state);
			if (!SupplicantState.COMPLETED.equals(state))
				return;
		}
		//拿到ssid
		WifiManager m = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
		String ssid = m.getConnectionInfo().getSSID();
		Log.i("Wifi", "ssid is " + ssid);
		if (ssid == null) return;
		//切换ssid对应的地址
		if (IPUtil.isStaticSsid(context, ssid)) {
			//切成配置的地址
			Log.i("Wifi", "ssid switch static");
			IpSetting setting = IPUtil.getIpSettingBySsid(context, ssid);
			switchToStatic(context, setting);
		} else {
			//切成dhcp的地址
			Log.i("Wifi", "ssid switch dhcp");
			switchToDhcp(context);
		}
	}
	
	public static void showErrorToast(Context context, String msg) {
		Toast
		.makeText(context, msg, Toast.LENGTH_LONG)
		.show();
	}
	
	private void switchToDhcp(Context context) {
		int c1 = Settings.System.getInt(context.getContentResolver(), Settings.System.WIFI_USE_STATIC_IP, 0);
		if (c1 == 0) return;
		
		Settings.System.putInt(
			context.getContentResolver(), Settings.System.WIFI_USE_STATIC_IP, 0);
	}

	private void switchToStatic(Context context, IpSetting setting) {
		int c1 = Settings.System.getInt(context.getContentResolver(), Settings.System.WIFI_USE_STATIC_IP, 0);
		String c2 = Settings.System.getString(context.getContentResolver(), Settings.System.WIFI_STATIC_IP);
		if (c1 == 1 && setting.ip.equals(c2)) return;
		
		Settings.System.putInt(
			context.getContentResolver(), Settings.System.WIFI_USE_STATIC_IP, 1);
		Settings.System.putString(
				context.getContentResolver(), Settings.System.WIFI_STATIC_IP, setting.ip);
		Settings.System.putString(
				context.getContentResolver(), Settings.System.WIFI_STATIC_NETMASK, setting.mask);
		Settings.System.putString(
				context.getContentResolver(), Settings.System.WIFI_STATIC_GATEWAY, setting.gateway);
		Settings.System.putString(
				context.getContentResolver(), Settings.System.WIFI_STATIC_DNS1, setting.dns1);
		Settings.System.putString(
				context.getContentResolver(), Settings.System.WIFI_STATIC_DNS2, setting.dns2);
	}
}
